<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Classes extends AdminController { 

	function __construct() {  
		parent::__construct();
		$this -> load -> model("Class_model");
		$this -> load -> library("ProjectService/class_service");
	} 

	function getDatas($params = array(), $isExport = false) {
	
		$connar = array();
		if (!$isExport) {
			$params = $_POST;
			$connar = $this -> Class_model -> pickPages($connar, $params);
		}
		$connar['items']="db_class.*,SUBSTRING(elective_lesson_no, 12,1) term";
		$connar['join']=array('db_admin','db_admin.usercode=db_class.teacher_id');	
		// $connar['where']=$this->sqlLikeEscape('teaching_plan_no like "%??%"',array(date('Y')));
		$connar['where']="1=1";		
		if (isset($params['code'])) {
			$connar['where'] .= $this->sqlLikeEscape(" and code like '%??%' ",array($params['code']));
		}
		if (isset($params['name'])) {
			$connar['where'] .= $this->sqlLikeEscape(" and name like '%??%' ",array($params['name']));
		}
		if (isset($params['professional_direction'])) {
			$connar['where'] .= $this->sqlLikeEscape(" and professional_direction like '%??%' ",array($params['professional_direction']));
		}
		if (isset($params['teacher_name'])) {
			$connar['where'] .= $this->sqlLikeEscape(" and db_admin.username like '%??%' ",array($params['teacher_name']));
		}
		if (isset($params['classroom'])) {
			$connar['where'] .= $this->sqlLikeEscape(" and classroom like '%??%' ",array($params['classroom']));
		}
		if (isset($params['classname'])) {
			$connar['where'] .= $this->sqlLikeEscape(" and classname like '%??%' ",array($params['classname']));
		}
		if (isset($params['course_units']) && $params['course_units']!=-1) {
			$connar['where'] .= $this->sqlLikeEscape(" and course_units like '%??%' ",array($params['course_units']));
		}
		if (isset($params['week'])) {
			$connar['where'] .= $this->sqlLikeEscape(" and start_time<=?? and end_time>=?? ",array($params['week'],$params['week']));
		}
		if (isset($params['academic_year'])) {
			$connar['where'] .= $this->sqlLikeEscape(" and elective_lesson_no like '%??%' ",array($params['academic_year']));
		}
		if(isset($params['weekday']) && $params['weekday']!=-1){
			$connar['where'].= $this->sqlLikeEscape(" and sksj like '%??%' ",array($params['weekday']));
		}
		if(isset($params['section']) && $params['section']!=-1){
			$connar['where'].= $this->sqlLikeEscape(" and sksj like '%??%' ",array($params['section']));
		}

		if(isset($params['term']) && $params['term']!=-1){
			$connar['where'].= $this->sqlLikeEscape(" and SUBSTRING(elective_lesson_no, 12,1)=?? ",array($params['term']));
		}

		

		if (!$isExport) {
			$data = $this -> Class_model -> get_page_list($connar);
		
		} else {
			$data["ret"] = $this -> Class_model -> get_list_full($connar);
		}
		foreach ($data['ret'] as $k => $v) {
			$teacher_name='';
			$teacher_id=explode(',',$v['teacher_id']);
			foreach ($teacher_id as $k1 => $v2) {
				$teacher=$this->Admin_model->get_single(array('usercode'=>$v2));
				if(strstr($teacher['role_id'], WebRole::ExternalSupervisor[XPHEnum::Value]) !==false or strstr($teacher['sub_role_id'], WebRole::ExternalSupervisor[XPHEnum::Value]) !==false ){
					$teacher_name.=$teacher['username'].'('.WebRole::ExternalSupervisor[XPHEnum::Caption].')';
				}else{
					$teacher_name.=$teacher['username'];
				}
				
			}
			$data['ret'][$k]['teacher_name']=$teacher_name;
			if ($v['term']=='1') {
				$data['ret'][$k]['term_char']='第一学期';
			}else{
				$data['ret'][$k]['term_char']='第二学期';
			}
		}
		if (!$isExport) {
			echo json_encode($data);
		} else {
			return $data["ret"];
		}
	}

	public function Index() { 
		    // 控制筛选项部门
        $deptlist=(new AdminDeptList)->getRemark()[1];
        $data['course_units'] = array();
        foreach($deptlist as $k=>$v){
        	$data['course_units'][$k]['course_units']=$v;
        }

		// $connar['items']='distinct(course_units)';
		// $connar['where']='course_units is not null';
		// $data['course_units']=$this->Class_model->get_list_full($connar);

		$connar1['items']='DISTINCT(SUBSTRING(elective_lesson_no, 2,9)) academic_year';
		$data['academic_year']=$this->Class_model->get_list_full($connar1);
		$date=date('Y-m-d',time());			
		$month=date('m',time());
		if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
			$year=date('Y',time());
			$year_last=date('Y',time())+1;
			$data['default_academic_year']=strval($year.'-'.$year_last);			
		}else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
			$year=date('Y',time())-1;
			$year_last=date('Y',time());
			$data['default_academic_year']=strval($year.'-'.$year_last);	
		}
		$this->load->view('admin/classList',$data);
	}

	
	function getClass(){
		$class=$this->class_service->getClass(time());
		exit(retJson("课程数据.", true,$class));
	}


	function showClass($class_id){
		$data['title']='课程详情';
		$data['class']=$this->class_service->getClassById($class_id);
		$this->load->view('admin/showClass',$data);
	}

	function classDetail($class_id){
		$data['class_id']=$class_id;	
		$this->load->view('admin/classDetail',$data);
	}
	function classDatas($class_id){
		$data=$this->Class_model->get_single(array('id'=>$class_id));
		
		$week=explode(';',$data['sksj']);
	
		$classroom=	explode(';',$data['classroom']);
		if(count($week)==1 && $week[0]==''){
			exit(retJson('没有课程安排',false));
		}
		for ($i=0; $i <7 ; $i++) { 
			for ($j=0; $j <12; $j++) { 
				$courseData[$i][$j]='';
			}
		}
		
		foreach ($week as $k => $v) {
			$weekday=substr($v,0,6);
			if($weekday=='周一'){
				$weekday_key=0;
			}else if($weekday=='周二'){
				$weekday_key=1;
			}else if($weekday=='周三'){
				$weekday_key=2;
			}else if($weekday=='周四'){
				$weekday_key=3;
			}else if($weekday=='周五'){
				$weekday_key=4;
			}else if($weekday=='周六'){
				$weekday_key=5;
			}else if($weekday=='周日'){
				$weekday_key=6;
			}
			$section=$this->get_string_between($v,'第','节');
			
			$isset = strstr($section, ',');
			if($isset){
				$section_key=explode(',',$section);
			}else{
				$section_key=explode('-',$section);
			}

			
			foreach ($section_key as $k1 => $v1) {
				$v1=intval($v1)-1;				
				if(isset($classroom[$k]) && $classroom[$k] != ""){
					$courseData[$weekday_key][$v1]=$data['name'].'('.$classroom[$k].')';
				}else{
					$courseData[$weekday_key][$v1]=$data['name'].'()';
				}
				
			}			
		}
		
		exit(retJson('返回课程数据',true,$courseData));
	}

	function get_string_between($string, $start, $end){
		$string = ' ' . $string;
		$ini = strpos($string, $start);
		if ($ini == 0) return '';
		$ini += strlen($start);
		$len = strpos($string, $end, $ini) - $ini;
		return substr($string, $ini, $len);
	}
	
}